package com.zealousbd.adserver.webapp.action;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.zealousbd.adserver.webapp.i18n.ApplicationResources;
import com.zealousbd.adserver.webapp.util.RequestUtil;

public class ReloadResourceMessageServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7742087806786651046L;

	protected final transient Log log = LogFactory
			.getLog(ReloadResourceMessageServlet.class);

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Locale userLocale = request.getLocale();
		Class klass = ResourceBundle.getBundle(ApplicationResources.class.getName(),userLocale).getClass();

		log.error(klass);
		while(klass != ResourceBundle.class) {
			klass = klass.getSuperclass();
			log.warn(klass);
		}
		
		Field field;
		try {
			field = klass.getDeclaredField("cacheList");
			field.setAccessible(true);
			sun.misc.SoftCache cache = (sun.misc.SoftCache)field.get(null);
			cache.clear();
			log.error("cache cleared......");
		} catch (SecurityException e) {
			log.error(e.getMessage());
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage());
		} catch (IllegalArgumentException e) {
			log.error(e.getMessage());
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		}

		response.sendRedirect(RequestUtil.getAppURL(request) + "/resource_reloaded.jsp");
//		RequestDispatcher dispatcher = request
//				.getRequestDispatcher("/resource_reloaded.jsp");
//		dispatcher.(request, response);
	}
}
